package com.cn.onetrip.rorate3d;

import com.cn.onetrip.activity.RouteAtlasActivity;
import com.cn.onetrip.activity.ScenicAtlasActivity;
import com.cn.onetrip.untility.ImageUtils;

import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.os.Build.VERSION;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.animation.Transformation;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class RouteAtlasGallery extends Gallery {

	private GestureDetector gestureScanner;
	private ScaleImageView imageView;
	// private ScaleImageView imageView2;
	private Context mContext;

	private int screenHeight, screenWidth;
	private Handler parentHandler = null;

	private LayoutInflater mInflater;

	public RouteAtlasGallery(Context context) {
		super(context);
		mContext = context;
		mInflater = LayoutInflater.from(context);
		setScreenSize();
		touchScale();
	}

	public RouteAtlasGallery(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
		mInflater = LayoutInflater.from(context);
		setScreenSize();
		touchScale();
	}

	public RouteAtlasGallery(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		mInflater = LayoutInflater.from(context);
		setScreenSize();
		touchScale();
	}

	// public void setAlphaPH(float alpha) {
	// RelativeLayout layout = (RelativeLayout) RouteAtlasGallery.this
	// .getSelectedView();
	// View view = layout.getChildAt(1);
	// if (view instanceof ScaleImageView) {
	// imageView2 = (ScaleImageView) view;
	// imageView2.setAlpha(alpha);
	// }
	// }

	private void touchScale() {
		gestureScanner = new GestureDetector(new MySimpleGesture());
		this.setOnTouchListener(new OnTouchListener() {
			// set the ImageView scale
			float baseValue;
			float originalScale;

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// RelativeLayout layout = (RelativeLayout)
				// RouteAtlasGallery.this
				// .getSelectedView();
				// View view = layout.getChildAt(0);
				// View view2 = layout.getChildAt(1);
				View view = RouteAtlasGallery.this.getSelectedView();

				if (view instanceof ScaleImageView) {
					imageView = (ScaleImageView) view;
					// imageView2 = (ScaleImageView) view2;
					if (event.getAction() == MotionEvent.ACTION_DOWN) {
						baseValue = 0;
						originalScale = imageView.getScale();

					}
					if (event.getAction() == MotionEvent.ACTION_MOVE) {
						if (event.getPointerCount() == 2) {
							float x = event.getX(0) - event.getX(1);
							float y = event.getY(0) - event.getY(1);
							float value = (float) Math.sqrt(x * x + y * y);
							if (baseValue == 0) {
								baseValue = value;
							} else {
								float scale = value / baseValue;
								// scale the image
								float scaleXY = originalScale * scale;
								float SETX = x + event.getX(1);
								float SETY = y + event.getY(1);
								imageView.zoomTo(scaleXY, SETX, SETY);
								// imageView2.zoomTo(scaleXY, SETX, SETY);

							}
						}
					}
				}
				return false;
			}

		});
	}

	public void setScreenSize() {
		DisplayMetrics dm = new DisplayMetrics();
		((Activity) mContext).getWindowManager().getDefaultDisplay()
				.getMetrics(dm);
		screenHeight = dm.heightPixels;
		// screenHeight = 1776;
		screenWidth = dm.widthPixels;

		// View v = ((Activity) mContext).getWindow().findViewById(
		// Window.ID_ANDROID_CONTENT);
		// screenHeight = v.getHeight();
		// screenWidth = v.getWidth();
		//
		// Log.i("screenWidth", "++++++++++" + screenWidth);
		// Log.i("screenHeight", "++++++++++" + screenHeight);
	}

	public void setHandler(Handler handler) {
		this.parentHandler = handler;
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		View view = RouteAtlasGallery.this.getSelectedView();
		// RelativeLayout layout = (RelativeLayout) RouteAtlasGallery.this
		// .getSelectedView();
		// View view = layout.getChildAt(0);
		// View view2 = layout.getChildAt(1);
		if (view instanceof ScaleImageView) {
			imageView = (ScaleImageView) view;
			// imageView2 = (ScaleImageView) view2;
			float v[] = new float[9];
			Matrix m = imageView.getImageMatrix();
			m.getValues(v);
			float left, right;
			float width, height, width2, height2;
			width = imageView.getScale() * imageView.getImageWidth();
			height = imageView.getScale() * imageView.getImageHeight();
			// width2 = imageView2.getScale() * imageView2.getImageWidth();
			// height2 = imageView2.getScale() * imageView2.getImageHeight();
			if ((int) width <= screenWidth && (int) height <= screenHeight) {
				Message msg = Message.obtain();
				msg.what = 3;
				parentHandler.sendMessage(msg);
				return super.onScroll(e1, e2, distanceX * 2.0f, distanceY);
			} else {
				left = v[Matrix.MTRANS_X];
				right = left + width;
				Rect r = new Rect();
				imageView.getGlobalVisibleRect(r);
				// imageView2.getGlobalVisibleRect(r);

				if (distanceX > 0) {
					if (r.left > 0) {
						super.onScroll(e1, e2, distanceX * 2.0f, distanceY);
					} else if (right <= screenWidth) {
						super.onScroll(e1, e2, distanceX * 2.0f, distanceY);
					} else {
						imageView.postTranslate(-distanceX * 2.0f, -distanceY);
						// imageView2.postTranslate(-distanceX, -distanceY);
					}
				} else if (distanceX < 0) {
					if (r.right < screenWidth) {
						super.onScroll(e1, e2, distanceX * 2.0f, distanceY);
					} else if (left >= 0) {
						super.onScroll(e1, e2, distanceX * 2.0f, distanceY);
					} else {
						imageView.postTranslate(-distanceX * 2.0f, -distanceY);
						// imageView2.postTranslate(-distanceX, -distanceY);
					}
				}
			}

		} else {
			super.onScroll(e1, e2, distanceX * 2.0f, distanceY);
		}
		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		return false;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		gestureScanner.onTouchEvent(event);
		switch (event.getAction()) {
		case MotionEvent.ACTION_UP:
			// RelativeLayout layout = (RelativeLayout) RouteAtlasGallery.this
			// .getSelectedView();
			// View view = layout.getChildAt(0);
			// View view2 = layout.getChildAt(1);

			View view = RouteAtlasGallery.this.getSelectedView();
			if (view instanceof ScaleImageView) {
				imageView = (ScaleImageView) view;
				// imageView2 = (ScaleImageView) view2;
				float width = imageView.getScale() * imageView.getImageWidth();
				float height = imageView.getScale()
						* imageView.getImageHeight();
				if ((int) width <= screenWidth && (int) height <= screenHeight) {
					break;
				}
				float v[] = new float[9];
				Matrix m = imageView.getImageMatrix();
				m.getValues(v);
				float top = v[Matrix.MTRANS_Y];
				float bottom = top + height;
				if (top > 0) {
					imageView.postTranslateDur(-top, 200f);
					// imageView2.postTranslateDur(-top, 200f);
				}
				Log.i("manga", "bottom:" + bottom);
				if (bottom < screenHeight) {
					imageView.postTranslateDur(screenHeight - bottom, 200f);
					// imageView2.postTranslateDur(screenHeight - bottom, 200f);
				}

			}
			break;
		}
		return super.onTouchEvent(event);
	}

	private class MySimpleGesture extends SimpleOnGestureListener {

		public boolean onDoubleTap(MotionEvent e) {
			View view = RouteAtlasGallery.this.getSelectedView();
			if (view instanceof ScaleImageView) {
				imageView = (ScaleImageView) view;
				if (imageView.getScale() > imageView.getScaleRate()) {
					imageView.zoomTo(imageView.getScaleRate(), screenWidth / 2,
							screenHeight / 2, 200f);
					// imageView.layoutToCenter();
				} else {
					imageView.zoomTo(1.0f, screenWidth / 2, screenHeight / 2,
							200f);
				}
			}
			return true;
		}

		@Override
		public boolean onSingleTapUp(MotionEvent e) {
			RouteAtlasActivity parentActivity = (RouteAtlasActivity) mContext;
			parentActivity.setTextViewState();
			return super.onSingleTapUp(e);
		}
	}
}
